package com.qfjy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.qfjy.client.VideoInfoFeignClient;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.service.MeetingPubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Slf4j
@RequestMapping("meetingPub")
//`@RefreshScope`  是 Spring Cloud 提供的一个注解，用于实现配置信息的动态刷新。当使用该注解标记在 Spring Bean 上时，该 Bean 的配置信息可以在运行时被动态刷新，而无需重启应用程序。
@RefreshScope
public class MeetingPubController {

    @Autowired
    private MeetingPubService meetingPubService;

    @Value("${info.version}")
    private String version;

    /**
     * TODO 根据会议编号查询会议信息  meetingPub/select?pcode=1111
     * @return
     */
    @ResponseBody
    @GetMapping("select")
    public Object selectMeetingPubByPcode(@RequestParam("pcode")String pcode){

       MeetingPub meetingPub= meetingPubService.selectMeetingPubByPcode(pcode);
       log.info("查询的是会议微服务信息：{}",meetingPub);



      /*Feign组件完成调用*/
       String videoResult= videoInfoFeignClient.selectVideoInfoByPcode(pcode);
        log.info("查询的是视频微服务结果--》{}",videoResult);


        meetingPub.setRemark(videoResult);
        meetingPub.setId(version);
       //要给的是一个ResultJson 统一内容格式（统一内容协议）
        return meetingPub;
    }

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private VideoInfoFeignClient videoInfoFeignClient;



    @GetMapping("info")
    @ResponseBody
    public String info(){

        return "Hello info";
    }

    @GetMapping("infoThread")
    @ResponseBody
    public String infoThread() throws InterruptedException {
        log.info("Hello infoThread");
        //线程休眠2秒钟
        //Thread.sleep(2000);
        return "Hello infoThread";
    }


    @GetMapping("info1")
    @ResponseBody
    @SentinelResource(value = "sentinel-/meetingPub/info1",blockHandler = "info1BlockHandler")
    public String info1(){
        log.info("info1 --->");
        return "Hello info1";
    }

    public String info1BlockHandler(BlockException ex){
        log.info(ex.getMessage());
        return "info1服务请求过多--》";
    }

    //流控 QPS每秒内只允许访问2次，超过2次，就不让访问。
    @GetMapping("info3")
    @ResponseBody
    @SentinelResource(value = "sentinel-/meetingPub/info3")
    public String info3(){
        log.info("info3 --->");
        return "Hello info3";
    }

}
